#!/bin/bash
###
# @Author: dvlproad dvlproad@163.com
# @Date: 2023-02-27 21:38:10
 # @LastEditors: dvlproad
 # @LastEditTime: 2023-06-09 14:40:52
# @FilePath: /AutoPackage-CommitInfo/bulidScriptCommon/upload/upload_result_log.sh
# @Description: 上传结束的各种log获取方法
###
# source ./upload_result_log.sh
# getUpdateLog_all -envInfoF "${ENVIRONMENT_FILE_PATH}" -comScriptHomeDir "${COMMON_SCRIPT_HOME_DIR_Absolute}"  --update-log-use-type "Notification"
# getUpdateLog_all -envInfoF "../example_packing_info/app_info.json" -comScriptHomeDir "../bulidScriptCommon"  --update-log-use-type "Notification"

# # 当前【shell脚本】的工作目录
# # $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
# CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
# #echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
# #bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute}/..
# bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..
# echo "bulidScriptCommon_dir_Absolute=${bulidScriptCommon_dir_Absolute}"

JQ_EXEC=$(which jq)

# 定义颜色常量
NC='\033[0m' # No Color
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'

function logSuccessMsg() {
    printf "${GREEN}$1${GREEN}\n${NC}"
}

function logMsg() {
    printf "$1\n${NC}"
}

function debug_log() {
    if [ "${isRelease}" == true ]; then
        echo "$1"
    fi
}

function logResutValue() {
    # ❌错误方法：使用echo无法正确输出值
    # echo "=============${escaped_value//\\/\\\\}============="

    # ✅正确方法：使用先存到file，再从file中打印
    local now_time=$(date +"%m%d%H%M%S")
    TempDir_Absolute="$( cd "$( dirname "$0" )" && pwd )"
    local temp_file_abspath="${TempDir_Absolute}/${now_time}.json"

    echo $1 > ${temp_file_abspath} # 创建文件，并写入内容到该文件。如果该文件已经存在，则会覆盖原有内容。
    cat ${temp_file_abspath}

    # 删除文件temp_file_abspath
    rm -rf ${temp_file_abspath}
}

# 获取安装包的各种路径信息
function getPackageFile_originPath_backupPath_cosUrl() {
    ENVIRONMENT_FILE_PATH=$1
    shouldMarkdown=$2

    Package_Url_Result=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} '.package_url_result')                           # 不能去除引号，不能下面取值会出错
    Package_Local_File_Url=$(echo ${Package_Url_Result} | ${JQ_EXEC} '.package_local_url' | sed 's/\"//g')          # 去除引号
    Package_Local_Backup_Dir=$(echo ${Package_Url_Result} | ${JQ_EXEC} '.package_local_backup_dir' | sed 's/\"//g') # 去除引号
    Package_Network_File_Url=$(echo ${Package_Url_Result} | ${JQ_EXEC} '.package_cos_url' | sed 's/\"//g')
    if [ "${isRelease}" == true ]; then
        echo "获取到之前已计算得到的安装包各种路径信息 ${BLUE}.package_url_result ${NC}的值(在文件 ${BLUE}${ENVIRONMENT_FILE_PATH} ${NC}中)如下:"
        cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} '.package_url_result'
    fi
    

    #echo "Package_Local_File_Url=${Package_Network_File_Url}"
    #echo "Package_Local_Backup_Dir=${Package_Local_Backup_Dir}"
    #echo "Package_Network_File_Url=${Package_Network_File_Url}"

    if [ -n "${Package_Local_File_Url}" ] && [ "${Package_Local_File_Url}" != "package local url" ]; then
        PackageDirOrFilePathResult="本地地址：${Package_Local_File_Url}"
        PackageDirOrFilePathResult_markdown="本地地址：[${Package_Local_File_Url}](${Package_Local_File_Url})"
    fi

    if [ -n "${Package_Local_Backup_Dir}" ] && [ "${Package_Local_Backup_Dir}" != "package local backup dir" ]; then
        PackageDirOrFilePathResult+="\n已备份到本地目录：${Package_Local_Backup_Dir}"
        PackageDirOrFilePathResult_markdown+="\n已备份到本地目录：[${Package_Local_Backup_Dir}](${Package_Local_Backup_Dir})"
    fi

    if [ -n "${Package_Network_File_Url}" ] && [ "${Package_Network_File_Url}" != "package cos url" ]; then
        PackageDirOrFilePathResult+="\ncos地址：${Package_Network_File_Url}"
        PackageDirOrFilePathResult_markdown+="\ncos地址：[${Package_Network_File_Url}](${Package_Network_File_Url})"
    fi
}

function getPackageFile_website() {
    ENVIRONMENT_FILE_PATH=$1
    bulidScriptCommon_dir_Absolute=$2 # 当是上传到蒲公英的时候，需要此值来获取蒲公英的各种参数
    shouldMarkdown=$3

    appOfficialWebsite_home=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} '.package_result.package_official_website' | sed 's/\"//g')
    appOfficialWebsiteResult="官网:${appOfficialWebsite_home}"
    appOfficialWebsiteResult_markdown="官网:[${appOfficialWebsite_home}](${appOfficialWebsite_home})"

    PackageTargetType=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} ".package_default_target" | sed 's/\"//g')
    if [ "${PackageTargetType}" == "dev" ]; then # 是蒲公英的时候才需要取验证
        source ${bulidScriptCommon_dir_Absolute}/a_function_result.sh
        getChannelValueFromFile -pagyerDownJsonF "${ENVIRONMENT_FILE_PATH}" -envJsonF "${ENVIRONMENT_FILE_PATH}"
        if [ $? != 0 ]; then
            echo "$FUNCNAME ❌Error:执行命令 getChannelValueFromFile 的时候，出错了"
            return 1
        fi

        appOfficialWebsite_pgyer=${packagePgyerChannelShortcutResult_upload}
        if [ "${appOfficialWebsite_pgyer}" != "null" ] && [ -n "${appOfficialWebsite_pgyer}" ] && [ "${appOfficialWebsite_pgyer}" != "unknow upload pgyer channelShortcut" ]; then
            appOfficialWebsite_pgyer="https://www.pgyer.com/${appOfficialWebsite_pgyer}"
            appOfficialWebsiteResult+="(实际:${appOfficialWebsite_pgyer})"
            appOfficialWebsiteResult_markdown+="(实际:[${appOfficialWebsite_pgyer}](${appOfficialWebsite_pgyer}))"
        else
            appOfficialWebsite_pgyer=""
        fi
    elif [ "${PackageTargetType}" == "inner" ] || [ "${PackageTargetType}" == "formal" ]; then
        package_download_url=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} '.package_result.package_download_website' | sed 's/\"//g')
        if [ -n "${package_download_url}" ] && [ "${package_download_url}" != "null" ] && [ "${package_download_url}" != "unknown download website" ]; then
            appOfficialWebsiteResult+="(实际:${package_download_url})"
            appOfficialWebsiteResult_markdown+="(实际:[${package_download_url}](${package_download_url}))"
        fi
    fi

    return 0
}

function getappUploadContinue_uploadCheck() {
    ENVIRONMENT_FILE_PATH=$1

    UPLOAD_FINISH_JSON_FILE=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r '.uploadSupplementJsonFile') # 上传结束后，要对各环境添加说明的文案json文件
    if [ ! -f "${UPLOAD_FINISH_JSON_FILE}" ]; then
        echo "⚠️Warning:您的环境文件${ENVIRONMENT_FILE_PATH}中的uploadSupplementJsonFile属性【上传结束后，要对各环境添加说明的文案json文件路径】的值${UPLOAD_FINISH_JSON_FILE} 未设置或不存在，等下会缺失这部分信息，若要使用，请检查补充"
        return 0
    fi

    PlatformType=$(cat $ENVIRONMENT_FILE_PATH | ${JQ_EXEC} -r .platform_type)
    PackageNetworkType=$(cat $ENVIRONMENT_FILE_PATH | ${JQ_EXEC} -r .package_default_env)
    PackageTargetType=$(cat $ENVIRONMENT_FILE_PATH | ${JQ_EXEC} -r .package_default_target)
    # echo "正在执行命令：《cat ${UPLOAD_FINISH_JSON_FILE} | ${JQ_EXEC} \".${PackageNetworkType}\" | ${JQ_EXEC} \".${PlatformType}\" | ${JQ_EXEC} \".${PackageTargetType}\"》"
    appUploadJson=$(cat ${UPLOAD_FINISH_JSON_FILE} | ${JQ_EXEC} ".${PackageNetworkType}" | ${JQ_EXEC} ".${PlatformType}" | ${JQ_EXEC} ".${PackageTargetType}")
    if [ $? != 0 ]; then
        echo "❌Error:获取上传后要提示的文案错误，请检查是否是使用jquery时候，传入的key不对，如key有空格等导致的错误"
        echo "附：=========获取到的环境信息在${ENVIRONMENT_FILE_PATH}中分别为PlatformType=${PlatformType},----PackageNetworkType=${PackageNetworkType},-----PackageTargetType=${PackageTargetType}"
        return 1
    fi
    if [ -z "${appUploadJson}" ]; then
        echo "友情提示：获取到的上传后要提示的文案信息为空(非错误,可能是该环境无需配置)"
        return 0
    fi
    # echo "获取到的上传后要提示的文案信息 appUploadJson=${appUploadJson}"

    # continue
    appUploadContinueJson=$(echo "${appUploadJson}" | ${JQ_EXEC} ".continue")
    # echo "appUploadContinueJson=${appUploadContinueJson}"

    appUploadContinueText=''

    appUploadContinueTitle=$(echo "${appUploadJson}" | ${JQ_EXEC} ".title")
    appUploadContinueText+=${appUploadContinueTitle}

    appUploadContinueSteps=$(echo "${appUploadJson}" | ${JQ_EXEC} ".step")
    appUploadContinueStepCount=$(echo "${appUploadJson}" | ${JQ_EXEC} ".step|length")
    for ((i = 0; i < appUploadContinueStepCount; i++)); do
        appUploadContinueStep=${appUploadContinueSteps}[$i]
        appUploadContinueText+=${appUploadContinueStep}
    done

    # check
    appUploadCheckJson=$(echo "${appUploadJson}" | ${JQ_EXEC} ".continue")

    appUploadCheckText=''

    appUploadCheckTitle=$(echo "${appUploadJson}" | ${JQ_EXEC} ".title")
    appUploadCheckText+=${appUploadCheckTitle}

    appUploadCheckSteps=$(echo "${appUploadJson}" | ${JQ_EXEC} ".step")
    appUploadCheckStepCount=$(echo "${appUploadJson}" | ${JQ_EXEC} ".step|length")
    for ((i = 0; i < appUploadCheckStepCount; i++)); do
        appUploadCheckStep=${appUploadCheckSteps}[$i]
        appUploadCheckText+=${appUploadCheckStep}
    done

    # continue + check
    appUploadContinue_uploadCheck_result=""
    if [ -n "${appUploadContinueText}" ] && [ "${appUploadContinueText}" != "null" ]; then
        appUploadContinue_uploadCheck_result+="${appUploadContinueText}"
    fi
    if [ -n "${appUploadCheckText}" ] && [ "${appUploadCheckText}" != "null" ]; then
        appUploadContinue_uploadCheck_result+="${appUploadCheckText}"
    fi
}

# 获取打包结果的头部数据，①头部字符串数组更新到文件(便于markdown下冗余文本太长需要截断)
function getUpdateLog_headString() {
    ENVIRONMENT_FILE_PATH=$1
    bulidScriptCommon_dir_Absolute=$2 # 当是上传到蒲公英的时候，需要此值来获取蒲公英的各种参数
    shouldMarkdown=$3
    CURRENT_HEADER_RESULT_FILE_PATH=$4

    if [ ! -f "${ENVIRONMENT_FILE_PATH}" ]; then
        echo "❌Error:您填写【保存打包信息文件】的文件${ENVIRONMENT_FILE_PATH} 未设置或不存在，请检查"
        return 1
    fi

    # 1、初始化
    PackageUploadHeaderTextResult=''
    Escape_UPDATEHEADER_ARRAY_ONLY_STRING=""

    PackageErrorCode=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r '.package_code')       # -r 去除字符串引号
    PackageErrorMessage=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r '.package_message') # -r 去除字符串引号
    debug_log "header错误码和错误信息分别为PackageErrorCode=${PackageErrorCode},PackageErrorMessage=${PackageErrorMessage}"

    # 2、添加各种值
    # ①失败的话，填写失败原因
    if [ "${PackageErrorCode}" != "package_code_0" ]; then
        PackageUploadHeaderTextResult+="\n原因：${PackageErrorMessage}"

        Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="\"" # 头部的双引号
        if [ -n "${Escape_UPDATEHEADER_ARRAY_ONLY_STRING}" ]; then
            Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="\n"
        fi
        Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="原因：${PackageErrorMessage}"
        Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="\"," # 尾部的双引号及逗号
    fi

    # ②本包文件的各种路径
    getPackageFile_originPath_backupPath_cosUrl "${ENVIRONMENT_FILE_PATH}" "${shouldMarkdown}"
    if [ $? != 0 ]; then
        echo "❌Error:执行 getPackageFile_originPath_backupPath_cosUrl 的时候出错"
    fi
    if [ -n "${PackageDirOrFilePathResult}" ]; then
        PackageUploadHeaderTextResult+="\n${PackageDirOrFilePathResult}"

        Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="\"" # 头部的双引号
        if [ -n "${Escape_UPDATEHEADER_ARRAY_ONLY_STRING}" ]; then
            Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="\n"
        fi
        if [ "${shouldMarkdown}" == "true" ]; then
            Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="${PackageDirOrFilePathResult_markdown}"
        else
            Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="${PackageDirOrFilePathResult}"
        fi
        Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="\"," # 尾部的双引号及逗号
    fi

    # ③官网路径(用于上传失败的时候，提示可去下载之前的包)
    getPackageFile_website "${ENVIRONMENT_FILE_PATH}" "${bulidScriptCommon_dir_Absolute}" "${shouldMarkdown}"
    if [ $? != 0 ]; then
        echo "❌Error:执行 getPackageFile_website 的时候出错"
        return 1
    fi
    if [ -n "${appOfficialWebsiteResult}" ]; then
        if [ "${PackageErrorCode}" != "package_code_0" ]; then
            PackageUploadHeaderTextResult+="找上个版本可去："
        fi
        PackageUploadHeaderTextResult+="${appOfficialWebsiteResult}。"

        Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="\"" # 头部的双引号
        if [ -n "${Escape_UPDATEHEADER_ARRAY_ONLY_STRING}" ]; then
            Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="\n"
        fi
        if [ "${PackageErrorCode}" != "package_code_0" ]; then
            Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="找上个版本可去："
        fi
        if [ "${shouldMarkdown}" == "true" ]; then
            Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="${appOfficialWebsiteResult_markdown}"
        else
            Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="${appOfficialWebsiteResult}"
        fi
        Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="\"," # 尾部的双引号及逗号
    fi

    # ④打包成功后的，后续其他操作提示文案
    if [ "${PackageErrorCode}" == "package_code_0" ]; then
        getappUploadContinue_uploadCheck "${ENVIRONMENT_FILE_PATH}"
        if [ $? != 0 ]; then
            echo "❌Error:执行命令 getappUploadContinue_uploadCheck 的时候，出错了"
            return 1
        fi
        if [ -n "$appUploadContinue_uploadCheck_result" ]; then
            PackageUploadHeaderTextResult+="\n${appUploadContinue_uploadCheck_result}。"
            Escape_UPDATEHEADER_ARRAY_ONLY_STRING+="\"${appUploadContinue_uploadCheck_result}\","
        fi
    fi
    # Escape_UPDATEHEADER_ARRAY_ONLY_STRING="\"我只是测试的内容：\","
    # echo "$FUNCNAME ✅✅✅✅✅✅✅✅${Escape_UPDATEHEADER_ARRAY_ONLY_STRING}"

    # 3、输出或保存结果
    # ①先去除最后一个字符,即逗号","
    if [ -n "${Escape_UPDATEHEADER_ARRAY_ONLY_STRING}" ]; then
        Escape_UPDATEHEADER_ARRAY_ONLY_STRING=${Escape_UPDATEHEADER_ARRAY_ONLY_STRING:0:${#Escape_UPDATEHEADER_ARRAY_ONLY_STRING}-1}
    fi

    # ②正式保存值
    # echo "💡💡💡💡💡💡💡💡PackageUploadHeaderTextResult=${PackageUploadHeaderTextResult}"
    if [ ! -f "${CURRENT_HEADER_RESULT_FILE_PATH}" ]; then
        printf "${RED}❌Error: 执行 $FUNCNAME 发生错误了。发现您的 CURRENT_HEADER_RESULT_FILE_PATH=${CURRENT_HEADER_RESULT_FILE_PATH}未设置或者不存在，请检查。。。。。。${NC}\n"
        return 1
    fi
    HEADER_SLICE_STRING_ARRAY_KEY="branch_info_result.${UPDATE_LOG_USE_TYPE}.header.slice"
    sh ${bulidScriptCommon_dir_Absolute}/update_json_file.sh -f "${CURRENT_HEADER_RESULT_FILE_PATH}" -sk "${HEADER_SLICE_STRING_ARRAY_KEY}" -sv "[${Escape_UPDATEHEADER_ARRAY_ONLY_STRING}]" --skip-value-check "true"
    if [ $? != 0 ]; then
        printf "${RED}❌Error: 在$FUNCNAME中更新打包结果的头部字符串数组到文件中，错误啦2。。。。。。${NC}\n"
        return 1
    fi
    # logSuccessMsg "恭喜:您获取到的头部数据(.header)markdown:${shouldMarkdown}的 ${PURPLE}.${HEADER_SLICE_STRING_ARRAY_KEY} ${GREEN}值(在 ${BLUE}${CURRENT_HEADER_RESULT_FILE_PATH} ${GREEN}文件中)如下:"
    # cat ${CURRENT_HEADER_RESULT_FILE_PATH} | jq ".${HEADER_SLICE_STRING_ARRAY_KEY}" | jq '.'
}

# 获取打包结果的尾部数据，①尾部字符串数组更新到文件(便于markdown下冗余文本太长需要截断)
function getUpdateLog_bottomString() {
    ENVIRONMENT_FILE_PATH=$1
    bulidScriptCommon_dir_Absolute=$2 # 当是上传到蒲公英的时候，需要此值来获取蒲公英的各种参数
    UPDATE_LOG_USE_TYPE=$3
    BOTTOM_SALED_IN_JSON_FILE_PATH=$4

    if [ ! -f "${ENVIRONMENT_FILE_PATH}" ]; then
        echo "❌Error:您填写【保存打包信息文件】的文件${ENVIRONMENT_FILE_PATH} 未设置或不存在，请检查"
        return 1
    fi

    # 1、初始化
    PackageUploadBottomTextResult=''
    Escape_UPDATEBOTTOM_ARRAY_ONLY_STRING=""

    PackageErrorCode=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r '.package_code')       # -r 去除字符串引号
    PackageErrorMessage=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r '.package_message') # -r 去除字符串引号
    debug_log "bottom错误码和错误信息分别为PackageErrorCode=${PackageErrorCode},PackageErrorMessage=${PackageErrorMessage}"

    # 2、添加各种值
    # ①成功的话，提醒下载看看
    if [ "${PackageErrorCode}" == "package_code_0" ]; then
        PackageUploadContentTextResult+="\n\n赶紧下载来看看有没有自己的问题吧🏃🏻‍♀️🏃🏻‍♀️🏃🏻‍♀️"
        Escape_UPDATEBOTTOM_ARRAY_ONLY_STRING+="\"\","
        Escape_UPDATEBOTTOM_ARRAY_ONLY_STRING+="\"赶紧下载来看看有没有自己的问题吧🏃🏻‍♀️🏃🏻‍♀️🏃🏻‍♀️\","
    fi
    # Escape_UPDATEBOTTOM_ARRAY_ONLY_STRING="\"我只是测试的内容：\","

    # 3、输出或保存结果
    # ①先去除最后一个字符,即逗号","
    if [ -n "${Escape_UPDATEBOTTOM_ARRAY_ONLY_STRING}" ]; then
        Escape_UPDATEBOTTOM_ARRAY_ONLY_STRING=${Escape_UPDATEBOTTOM_ARRAY_ONLY_STRING:0:${#Escape_UPDATEBOTTOM_ARRAY_ONLY_STRING}-1}
    fi

    # ②正式保存值
    # echo "💡💡💡💡💡💡💡💡PackageUploadBottomTextResult=${PackageUploadBottomTextResult}"
    BOTTOM_SLICE_STRING_ARRAY_KEY="branch_info_result.${UPDATE_LOG_USE_TYPE}.bottom.slice"
    sh ${bulidScriptCommon_dir_Absolute}/update_json_file.sh -f "${BOTTOM_SALED_IN_JSON_FILE_PATH}" -sk "${BOTTOM_SLICE_STRING_ARRAY_KEY}" -sv "[${Escape_UPDATEBOTTOM_ARRAY_ONLY_STRING}]" --skip-value-check "true"
    if [ $? != 0 ]; then
        echo "$FUNCNAME ❌更新打包结果的尾部字符串数组到文件中，错误啦2。。。。。。"
        return 1
    fi
}

# 获取打包结果的中部数据，①中部字符串数组更新到文件(便于markdown下冗余文本太长需要截断)
function getUpdateLog_contentString() {
    ENVIRONMENT_FILE_PATH=$1
    bulidScriptCommon_dir_Absolute=$2
    CURRENT_LASTONLINE_BRANCH_RESULT_FILE_PATH=$3

    PackageUploadContentTextResult=''
    # Escape_UPDATECONTENT_ARRAY_ONLY_STRING=""

    # brances_info_log.sh 在已执行过一次，所以这里不用重复执行，故去掉
    # REQUEST_FOR_LOG_TYPE_ARRAY=("Notification" "Pgyer")
    # sh ${bulidScriptCommon_dir_Absolute}/brances_info/brances_info_log/brances_info_log.sh -envInfoF "${ENVIRONMENT_FILE_PATH}" --request-for-log-types "${REQUEST_FOR_LOG_TYPE_ARRAY[*]}" -comScriptHomeDir "${bulidScriptCommon_dir_Absolute}" -resultSaveToJsonF "${CURRENT_LASTONLINE_BRANCH_RESULT_FILE_PATH}"
    # if [ $? != 0 ]; then
    #     return 1
    # fi
    Notification_All_Text_KEY="branch_info_result.Notification.all.text"
    fileValue_origin_withDoubleQuote=$(cat ${CURRENT_LASTONLINE_BRANCH_RESULT_FILE_PATH} | ${JQ_EXEC} ".${Notification_All_Text_KEY}")
    if [ "${isRelease}" == true ]; then
        echo "获取到之前已计算得到的安装包所含的所有分支信息 ${BLUE}${Notification_All_Text_KEY} ${NC}的值(在文件 ${BLUE}${CURRENT_LASTONLINE_BRANCH_RESULT_FILE_PATH} ${NC}中)如下:"
        cat ${CURRENT_LASTONLINE_BRANCH_RESULT_FILE_PATH} | jq ".${Notification_All_Text_KEY}" | jq '.'
    fi
    

    #echo "======fileValue_origin_withDoubleQuote=${fileValue_origin_withDoubleQuote}"
    # echo "======fileValue_origin_withDoubleQuote_echo=${fileValue_origin_withDoubleQuote//\\n/\\\\n}" # 这里转义换行符只是为了 echo 显示而已，没其他用处

    fileValue_origin_noDoubleQuote=${fileValue_origin_withDoubleQuote:1:${#fileValue_origin_withDoubleQuote}-2}
    #echo "======fileValue_origin_noDoubleQuote=${fileValue_origin_noDoubleQuote}"
    # echo "======fileValue_origin_noDoubleQuote_echo=${fileValue_origin_noDoubleQuote//\\n/\\\\n}" # 这里转义换行符只是为了 echo 显示而已，没其他用处

    notiChangeLog=${fileValue_origin_noDoubleQuote}
    #echo "notiChangeLog=\n${notiChangeLog}"
    if [ -n "${notiChangeLog}" ]; then
        PackageErrorCode=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r '.package_code') # -r 去除字符串引号
        if [ "${PackageErrorCode}" == "package_code_0" ]; then
            if [ -n "$notiChangeLog" ]; then
                PackageUploadContentTextResult+="\n更新内容：\n${notiChangeLog}"
            fi
        else
            if [ -n "$notiChangeLog" ]; then
                PackageUploadContentTextResult+="\n尝试更新内容：\n${notiChangeLog}"
            fi
        fi
    fi

    # logSuccessMsg "恭喜:您获取到的中部数据(.content)markdown:${shouldMarkdown}的 ${PURPLE}.${HEADER_SLICE_STRING_ARRAY_KEY} ${GREEN}值(在 ${BLUE}${CURRENT_HEADER_RESULT_FILE_PATH} ${GREEN}文件中)如下:"
    # cat ${CURRENT_HEADER_RESULT_FILE_PATH} | jq ".${HEADER_SLICE_STRING_ARRAY_KEY}" | jq '.'

    
    return 0
}

function getUpdateLog_all() {
    # shell 参数具名化
    show_usage="args: [-envInfoF, -comScriptHomeDir]\
                                      [--environment-info-json-file=, --common-script-home-dir=]"

    while [ -n "$1" ]; do
        case "$1" in
        -envInfoF | --environment-info-json-file)
            ENVIRONMENT_FILE_PATH=$2
            shift 2
            ;;
        -comScriptHomeDir | --common-script-home-dir)
            COMMON_SCRIPT_HOME_DIR_Absolute=$2
            shift 2
            ;;
        -t | --update-log-use-type)
            UPDATE_LOG_USE_TYPE=$2
            shift 2
            ;;
        --) break ;;
        *)
            echo $1,$2,$show_usage
            break
            ;;
        esac
    done

    if [ -z "${UPDATE_LOG_USE_TYPE}" ]; then
        printf "${RED}❌Error: $FUNCNAME 日志使用场景入参不能为空${NC}\n"
        return 1
    fi

    RESULT_SALE_TO_JSON_FILE_PATH=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r '.branchsResultJsonFile')
    if [ ! -f "${RESULT_SALE_TO_JSON_FILE_PATH}" ]; then
        printf "${RED}❌Error: 执行 $FUNCNAME 发生错误了。提前发现您等下要用来保存结果的文件 RESULT_SALE_TO_JSON_FILE_PATH=${RESULT_SALE_TO_JSON_FILE_PATH} 未设置或者不存在，请检查。。。。。。${NC}\n"
        return 1
    fi

    PackageUploadResultText=''
    intercept_SLICE_STRING_ARRAY_ONLY_STRING=''

    if [ ! -f "${ENVIRONMENT_FILE_PATH}" ]; then
        printf "${RED}❌Error: $FUNCNAME 您填写【保存打包信息文件】的文件${ENVIRONMENT_FILE_PATH} 未设置或不存在，请检查。${NC}\n"
        return 1
    fi

    # 是 通知，且允许 markdown 的时候才能真正去尝试markdown
    NotificationMESSAGETYPE=$(cat $ENVIRONMENT_FILE_PATH | jq -r '.package_noti_msgtype')
    if [ "${UPDATE_LOG_USE_TYPE}" == "Notification" ] && [ "${NotificationMESSAGETYPE}" == "markdown" ]; then
        shouldMarkdown="true"
    fi

    # 1、获取头字符串 header
    printf "${PURPLE}正在执行方法(获取头字符串 header)：《 ${BLUE}getUpdateLog_headString \"${ENVIRONMENT_FILE_PATH}\" \"${COMMON_SCRIPT_HOME_DIR_Absolute}\" \"${shouldMarkdown}\" \"${RESULT_SALE_TO_JSON_FILE_PATH}\" ${PURPLE}》${NC}\n"
    getUpdateLog_headString "${ENVIRONMENT_FILE_PATH}" "${COMMON_SCRIPT_HOME_DIR_Absolute}" "${shouldMarkdown}" "${RESULT_SALE_TO_JSON_FILE_PATH}"
    if [ $? != 0 ]; then
        printf "${RED}❌Error:执行命令 getUpdateLog_headString 的时候，出错了${NC}\n"
        return 1
    fi
    PackageUploadResultText+=${PackageUploadHeaderTextResult}
    # intercept_updateContentString="${Escape_UPDATEHEADER_ARRAY_ONLY_STRING}," # 前后已有上引号,这是第一个，不可能就超过4096最大值，所以直接添加。而不用像bottom那样从文件中添加
    # intercept_SLICE_STRING_ARRAY_ONLY_STRING="${Escape_UPDATEHEADER_ARRAY_ONLY_STRING}"
    if [ "${shouldMarkdown}" == "true" ]; then
        intercept_updateContentString+="\n"
    fi
    HEADER_SLICE_STRING_ARRAY_KEY="branch_info_result.${UPDATE_LOG_USE_TYPE}.header.slice"
    debug_log "正在执行方法(截取带拼接的字符串 header):《 ${BLUE}getNewByDealCurrentWithAdd -wait \"${intercept_updateContentString}\" -current \"${intercept_SLICE_STRING_ARRAY_ONLY_STRING}\" -jsonF \"${RESULT_SALE_TO_JSON_FILE_PATH}\" -k \"${HEADER_SLICE_STRING_ARRAY_KEY}\" --is-last-append \"ture\" ${NC}》${NC}"
    getNewByDealCurrentWithAdd -wait "${intercept_updateContentString}" -current "${intercept_SLICE_STRING_ARRAY_ONLY_STRING}" -jsonF "${RESULT_SALE_TO_JSON_FILE_PATH}" -k "${HEADER_SLICE_STRING_ARRAY_KEY}" --is-last-append "false"
    intercept_updateContentString="${NEW_WAIT_INTERCEPT_STRING}"
    intercept_SLICE_STRING_ARRAY_ONLY_STRING="${NEW_OK_INTERCEPT_SLICE_STRING}"
    logResutValue "${CYAN}>>>>>>友情提示，添加 header 后，【临时的所有信息】如下>>>>>>\n${BLUE}${NEW_WAIT_INTERCEPT_STRING}${NC}\n${CYAN}<<<<<<友情提示，添加 header 后，【临时的所有信息】如上<<<<<<${NC}"

    # 2、获取正文字符串 content
    echo "${PURPLE}正在执行方法(获取正文字符串 content):《 ${BLUE}getUpdateLog_contentString \"${ENVIRONMENT_FILE_PATH}\" \"${COMMON_SCRIPT_HOME_DIR_Absolute}\" \"${RESULT_SALE_TO_JSON_FILE_PATH}\" ${PURPLE}》${NC}"
    getUpdateLog_contentString "${ENVIRONMENT_FILE_PATH}" "${COMMON_SCRIPT_HOME_DIR_Absolute}" "${RESULT_SALE_TO_JSON_FILE_PATH}"
    if [ $? != 0 ]; then
        echo "❌Error:执行命令 getUpdateLog_contentString 的时候，出错了"
        return 1
    fi
    PackageUploadResultText+=${PackageUploadContentTextResult}
    CONTENT_SLICE_STRING_ARRAY_KEY="branch_info_result.${UPDATE_LOG_USE_TYPE}.all.slice"
    debug_log "正在执行方法(截取带拼接的字符串 content):《 ${BLUE}getNewByDealCurrentWithAdd -wait \"${intercept_updateContentString}\" -current \"${intercept_SLICE_STRING_ARRAY_ONLY_STRING}\" -jsonF \"${RESULT_SALE_TO_JSON_FILE_PATH}\" -k \"${CONTENT_SLICE_STRING_ARRAY_KEY}\" --is-last-append \"false\" ${NC}》${NC}"
    getNewByDealCurrentWithAdd -wait "${intercept_updateContentString}" -current "${intercept_SLICE_STRING_ARRAY_ONLY_STRING}" -jsonF "${RESULT_SALE_TO_JSON_FILE_PATH}" -k "${CONTENT_SLICE_STRING_ARRAY_KEY}" --is-last-append "false"
    intercept_updateContentString="${NEW_WAIT_INTERCEPT_STRING}"
    intercept_SLICE_STRING_ARRAY_ONLY_STRING="${NEW_OK_INTERCEPT_SLICE_STRING}"
    logResutValue "${CYAN}>>>>>>友情提示，添加 content 后，【临时的所有信息】如下>>>>>>\n${BLUE}${NEW_WAIT_INTERCEPT_STRING}${NC}\n${CYAN}<<<<<<友情提示，添加 content 后，【临时的所有信息】如上<<<<<<${NC}"

    # 3、获取尾部字符串 bottom
    echo "${PURPLE}正在执行方法(获取尾部字符串 bottom)：《 ${BLUE}getUpdateLog_bottomString \"${ENVIRONMENT_FILE_PATH}\" \"${COMMON_SCRIPT_HOME_DIR_Absolute}\" \"${RESULT_SALE_TO_JSON_FILE_PATH}\" ${PURPLE}》${NC}"
    getUpdateLog_bottomString "${ENVIRONMENT_FILE_PATH}" "${COMMON_SCRIPT_HOME_DIR_Absolute}" "${UPDATE_LOG_USE_TYPE}" "${RESULT_SALE_TO_JSON_FILE_PATH}"
    if [ $? != 0 ]; then
        echo "❌Error:执行命令 getUpdateLog_bottomString 的时候，出错了"
        return 1
    fi
    PackageUploadResultText+=${PackageUploadBottomTextResult}
    # intercept_updateContentString+="${Escape_UPDATEBOTTOM_ARRAY_ONLY_STRING}," # 📢:这不是第一个，直接添加上去可能就超过4096最大值，所以我们从文件中添加
    BOTTOM_SLICE_STRING_ARRAY_KEY="branch_info_result.${UPDATE_LOG_USE_TYPE}.bottom.slice"
    debug_log "正在执行方法(截取带拼接的字符串 bottom):《 ${BLUE}getNewByDealCurrentWithAdd -wait \"${intercept_updateContentString}\" -current \"${intercept_SLICE_STRING_ARRAY_ONLY_STRING}\" -jsonF \"${RESULT_SALE_TO_JSON_FILE_PATH}\" -k \"${BOTTOM_SLICE_STRING_ARRAY_KEY}\" --is-last-append \"ture\" ${NC}》${NC}"
    getNewByDealCurrentWithAdd -wait "${intercept_updateContentString}" -current "${intercept_SLICE_STRING_ARRAY_ONLY_STRING}" -jsonF "${RESULT_SALE_TO_JSON_FILE_PATH}" -k "${BOTTOM_SLICE_STRING_ARRAY_KEY}" --is-last-append "true"
    intercept_updateContentString="${NEW_WAIT_INTERCEPT_STRING}"
    intercept_SLICE_STRING_ARRAY_ONLY_STRING="${NEW_OK_INTERCEPT_SLICE_STRING}"
    logResutValue "${CYAN}>>>>>>友情提示，添加 bottom 后，【最新的所有信息】如下>>>>>>\n${BLUE}${NEW_OK_INTERCEPT_SLICE_STRING}${NC}\n${CYAN}<<<<<<友情提示，添加 bottom 后，【最新的所有信息】如上<<<<<<${NC}"

    # 4、保存值
    # intercept_SLICE_STRING_ARRAY_ONLY_STRING="123"

    # ②正式保存至
    intercept_SLICE_STRING_ARRAY_KEY="branch_info_result.${UPDATE_LOG_USE_TYPE}.all.intercept"
    Escape_UPDATECONTENT_ARRAY_brackets_STRING="[${intercept_SLICE_STRING_ARRAY_ONLY_STRING}]" # 内部已符合转义并加了逗号,现在补充上前后的[]
    debug_log "正在执行命令(保留各个值):《 sh ${COMMON_SCRIPT_HOME_DIR_Absolute}/update_json_file.sh -f \"${RESULT_SALE_TO_JSON_FILE_PATH}\" -sk \"${intercept_SLICE_STRING_ARRAY_KEY}\" -sv \"${Escape_UPDATECONTENT_ARRAY_brackets_STRING}\" --skip-value-check \"true\" 》"
    sh ${COMMON_SCRIPT_HOME_DIR_Absolute}/update_json_file.sh -f "${RESULT_SALE_TO_JSON_FILE_PATH}" -sk "${intercept_SLICE_STRING_ARRAY_KEY}" -sv "${Escape_UPDATECONTENT_ARRAY_brackets_STRING}" --skip-value-check "true"
    if [ $? != 0 ]; then
        echo "$FUNCNAME ❌更新打包结果的尾部字符串数组到文件中，错误啦2。。。。。。"
        return 1
    fi

    logSuccessMsg "恭喜:您获取到并保存到文件的最后所有数据在进行截断处理后(.all.intercept)markdown:${shouldMarkdown}的 ${PURPLE}.${intercept_SLICE_STRING_ARRAY_KEY} ${GREEN}值(在 ${BLUE}${RESULT_SALE_TO_JSON_FILE_PATH} ${GREEN}文件中)如下:"
    cat ${RESULT_SALE_TO_JSON_FILE_PATH} | jq ".${intercept_SLICE_STRING_ARRAY_KEY}" | jq '.'

    # echo "PackageUploadResultText=${PackageUploadResultText}"
}

# 对当前的①截断数组字符串和②待截断的字符串，进行添加JSON文件中指定的key的值，得到新的①截断数组字符串和②待截断的字符串
function getNewByDealCurrentWithAdd() {
    # shell 参数具名化
    show_usage="args: [-wait, -current, -jsonF, -k]\
                                      [--wait-intercept-string=, --current-no-bracket-array-string=, --json-file=, --array-key=]"

    while [ -n "$1" ]; do
        case "$1" in
        -wait | --wait-intercept-string)
            intercept_updateContentString=$2
            shift 2
            ;;
        -current | --current-no-bracket-array-string)
            intercept_SLICE_STRING_ARRAY_ONLY_STRING=$2
            shift 2
            ;;
        -jsonF | --json-file)
            RESULT_SALE_TO_JSON_FILE_PATH=$2
            shift 2
            ;;
        -k | --array-key)
            SLICE_STRING_ARRAY_KEY=$2
            shift 2
            ;;
        -isLast | --is-last-append)
            IS_LAST_APPEND=$2
            shift 2
            ;; # 是否是最后的拼接，如果拼接结束，不管是否超过长度，都将待处理的作为最终结果
        --) break ;;
        *)
            echo $1,$2,$show_usage
            break
            ;;
        esac
    done

    if [ ! -f "${RESULT_SALE_TO_JSON_FILE_PATH}" ]; then
        echo "${RED}要添加哪个文件不能为空${NC}\n"
        return 1
    fi

    if [ -z "${SLICE_STRING_ARRAY_KEY}" ]; then
        printf "${RED}❌Error:要取文件中哪个key的值 SLICE_STRING_ARRAY_KEY=${SLICE_STRING_ARRAY_KEY} 不能为空，请检查。${NC}\n"
        return 1
    fi

    if [ -z "${IS_LAST_APPEND}" ]; then
        printf "${RED}❌Error:是否是最后一次拼接的入参不能不填，否则无法正常结束。(①需要作为结束标记；②需要对结束时候删除最后尾部的逗号)。${NC}\n"
        return 1
    fi
    if [ "${IS_LAST_APPEND}" != "true" ] && [ "${IS_LAST_APPEND}" != "false" ]; then
        printf "${RED}❌Error:IS_LAST_APPEND 值只能为 true 或 false, 请检查！${NC}\n"
        return 1
    fi

    NEW_WAIT_INTERCEPT_STRING=${intercept_updateContentString}
    NEW_OK_INTERCEPT_SLICE_STRING=${intercept_SLICE_STRING_ARRAY_ONLY_STRING}
    # echo "=====截取带拼接的前①如下：NEW_OK_INTERCEPT_SLICE_STRING=${NEW_OK_INTERCEPT_SLICE_STRING}"
    # echo "=====截取带拼接前②如下：NEW_WAIT_INTERCEPT_STRING=${NEW_WAIT_INTERCEPT_STRING}"

    # echo "测试输出结果命令：《cat ${RESULT_SALE_TO_JSON_FILE_PATH} | ${JQ_EXEC} \".${SLICE_STRING_ARRAY_KEY}|length\"》"
    contentCount=$(cat ${RESULT_SALE_TO_JSON_FILE_PATH} | ${JQ_EXEC} ".${SLICE_STRING_ARRAY_KEY}|length")
    # echo "--------contentCount=${contentCount}"
    if [ "${contentCount}" == 0 ]; then
        echo "友情提示💡💡💡：${SLICE_STRING_ARRAY_KEY}没有内容"
        return 1
    fi

    for ((i = 0; i < contentCount; i++)); do
        iContent=$(cat ${RESULT_SALE_TO_JSON_FILE_PATH} | ${JQ_EXEC} ".${SLICE_STRING_ARRAY_KEY}" | ${JQ_EXEC} ".[${i}]")
        iContent_noQuote=${iContent:1:${#iContent}-2} # 去除前后的双引号,方便添加或修改
        # echo "--------iContent_noQuote=${iContent_noQuote}"

        newTextLength=$((${#NEW_WAIT_INTERCEPT_STRING} + ${#iContent_noQuote} + 1))
        # echo "--------如果添加新文本后 NEW_WAIT_INTERCEPT_STRING 长度会变为${newTextLength}"
        if [ ${newTextLength} -lt 2048 ]; then #企业微信通知最大长度为4096
            # 没超过长度限制的话，可以一直添加
            NEW_WAIT_INTERCEPT_STRING="${NEW_WAIT_INTERCEPT_STRING}${iContent_noQuote}\n"
        else
            # 超过长度限制的话，先把当前可以截断的保存下来。然后本次的这个就作为新的值,然后继续找下一个是否可以也拼起来
            echo "-----------------------长度超过限制了-----------------------"
            NEW_OK_INTERCEPT_SLICE_STRING+="\"${NEW_WAIT_INTERCEPT_STRING}\","
            NEW_WAIT_INTERCEPT_STRING="${iContent_noQuote}\n"
        fi
    done

    if [ "${IS_LAST_APPEND}" == "true" ]; then
        debug_log "💡💡💡友情提示：这是最后一次拼接，将不管是否超过长度，都将待处理的作为最终结果。附待处理值NEW_WAIT_INTERCEPT_STRING=${NEW_WAIT_INTERCEPT_STRING}"
        NEW_OK_INTERCEPT_SLICE_STRING+="\"${NEW_WAIT_INTERCEPT_STRING}\","
        NEW_WAIT_INTERCEPT_STRING=""

        # ①先清理冗余值
        # 去除最后一个字符,即逗号","
        if [ -n "${NEW_OK_INTERCEPT_SLICE_STRING}" ]; then
            NEW_OK_INTERCEPT_SLICE_STRING=${NEW_OK_INTERCEPT_SLICE_STRING:0:${#NEW_OK_INTERCEPT_SLICE_STRING}-1}
        fi
    fi

    # echo "=====截取带拼接的结果:IS_LAST_APPEND=${IS_LAST_APPEND}"
    # echo "=====截取带拼接的结果①如下：NEW_OK_INTERCEPT_SLICE_STRING=${NEW_OK_INTERCEPT_SLICE_STRING}"
    # echo "=====截取带拼接的结果②如下：NEW_WAIT_INTERCEPT_STRING=${NEW_WAIT_INTERCEPT_STRING}"
    # if [ "${IS_LAST_APPEND}" == "true" ]; then
    #     logResutValue "${CYAN}>>>>>>友情提示，添加后，【最新的所有信息】如下>>>>>>\n${BLUE}${NEW_OK_INTERCEPT_SLICE_STRING}${NC}\n${CYAN}<<<<<<友情提示，添加后，【最新的所有信息】如上<<<<<<${NC}"
    # else
    #     logResutValue "${CYAN}>>>>>>友情提示，添加后，【临时的所有信息】如下>>>>>>\n${BLUE}${NEW_WAIT_INTERCEPT_STRING}${NC}\n${CYAN}<<<<<<友情提示，添加后，【临时的所有信息】如上<<<<<<${NC}"
    # fi
}
